# Maintainer: Antonio Rojas <arojas@archlinux.org>
# Contributor: Ronald van Haren <ronald.archlinux.org>
# Contributor: damir <damir@archlinux.org>
# Contributor: Tom Newsom <Jeepster@gmx.co.uk>

pkgbase=gnuplot
pkgname=(gnuplot gnuplot-demos)
pkgver=5.4.6
pkgrel=1
pkgdesc='Plotting package which outputs to X11, PostScript, PNG, GIF, and others' 
arch=(x86_64)
url='http://www.gnuplot.info'
license=(custom)
depends=(gd wxwidgets-gtk3 lua qt5-svg libcerf libcaca) 
makedepends=(texlive-core emacs texlive-latexextra qt5-tools)
source=(https://downloads.sourceforge.net/sourceforge/$pkgname/$pkgname-$pkgver.tar.gz
        lua53_compat.patch)
sha256sums=('02fc27918200ed64d8f0c3b84fe81b95b59cd47ad99f270939ae497c19f27419'
            'bfd8a61abbf4491c74225cb9fd252619d4fc29751838bcb4c0639ffe05a00695')

prepare() {
  cd $pkgname-$pkgver

  # fix default source location; use the GDFONTPATH variable to modify at runtime
  sed -i 's|/usr/X11R6/lib/X11/fonts/truetype|/usr/share/fonts/TTF|' src/variable.c

  sed -i -e 's|/usr/X11R6/lib/X11/fonts/Type1|/usr/share/fonts/Type1|' \
  -e 's|$(X11ROOT)/X11R6/lib/X11/fonts/Type1|$(X11ROOT)/usr/share/fonts/Type1|' \
  src/variable.c

  patch -p1 < "$srcdir"/lua53_compat.patch
}

build() {
  cd $pkgbase-$pkgver

  MAKEINFO=/usr/bin/makeinfo \
  ./configure --prefix=/usr \
              --libexecdir=/usr/bin \
              --with-gihdir=/usr/share/gnuplot \
              --with-readline=gnu \
              --with-bitmap-terminals \
              --with-wx-single-threaded \
              --with-caca \
              --with-texdir=/usr/share/texmf/tex/latex/gnuplot
  make pkglibexecdir=/usr/bin
}

package_gnuplot() {
  cd $pkgbase-$pkgver
  make pkglibexecdir=/usr/bin DESTDIR="$pkgdir" install
  make DESTDIR="$pkgdir" install -C demo

  install -Dm644 Copyright -t "$pkgdir"/usr/share/licenses/$pkgname

  rm -f "$pkgdir"/usr/share/texmf-dist/ls-R
}

package_gnuplot-demos() {
  pkgdesc='Demos for gnuplot'
  depends=(gnuplot)

  install -Dm644 $pkgbase-$pkgver/demo/*.{dem,dat} -t "$pkgdir"/usr/share/gnuplot/demos
}
